/**********************************************************************
 *** *                                                            * ***
 *** *  Copyright (c) 2013 Harmonia Holdings Group LLC            * ***
 *** *                                                            * ***
 *** * Permission is hereby granted to use, modify, copy, and     * ***
 *** * distribute this software and its documentation for any     * ***
 *** * purpose only without profit, provided that the above       * ***
 *** * Copyright Notice appears in all copies and that both the   * ***
 *** * Copyright Notice and this Permission Notice appears in     * ***
 *** * every copy of supporting documentation.  No title to nor   * ***
 *** * ownership of the software is transferred hereby.  The name * ***
 *** * of Harmonia Holdings Group LLC shall not be used in        * ***
 *** * advertising or publicity related to the distribution of    * ***
 *** * the software without specific, written, prior permission.  * ***
 *** * This software is provided as-delivered without expressed   * ***
 *** * or implied warranty.  Harmonia Holdings Group LLC          * ***
 *** * makes no representation about the suitability of this      * ***
 *** * software for any purpose and accepts no responsibility for * ***
 *** * its use.                                                   * ***
 *** *                                                            * ***
 *** ************************************************************** ***
 *** *                                                            * ***
 *** * This program is free software; you can redistribute it     * ***
 *** * and/or modify it under the terms of the GNU General Public * ***
 *** * License as published by the Free Software Foundation;      * ***
 *** * either version 2 of the License, or (at your option) any   * ***
 *** * later version.                                             * ***
 *** *                                                            * ***
 *** * This program is distributed in the hope that it will be    * ***
 *** * useful, but WITHOUT ANY WARRANTY; without even the implied * ***
 *** * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR    * ***
 *** * PURPOSE.  See the GNU General Public License for more      * ***
 *** * details.                                                   * ***
 *** *                                                            * ***
 *** * You should have received a copy of the GNU General Public  * ***
 *** * License along with this program; if not, write to the Free * ***
 *** * Software Foundation, Inc., 51 Franklin Street, Fifth       * ***
 *** * Floor, Boston, MA 02110-1301, USA.                         * ***
 *** *                                                            * ***
 *** * For more information: http://www.gnu.org/licenses/gpl.html * ***
 *** *                                                            * ***
 **********************************************************************/
package org.etexascode.interrep.datamodel.utils;

public class UtilsUnitConversion {

	/**
	 * Converts the heading from a BSM heading to degrees azimuth.
	 * @param heading The angle of the car in 0.0125 degrees clockwise from North.
	 * @return The angle of the car in degrees clockwise from North.
	 */
	public static double convertBSMHeadingToDegrees(int heading) {
		return (heading * 0.0125) % 360.0;
	}

	/**
	 * Converts values in centimeters to feet
	 * @param inCentimeters - the value in centimeters
	 * @return - the value in feet
	 */
	public static double convertCentimetersToFeet(double inCentimeters) {
		return inCentimeters * 0.0328084; // Value according to Google's length conversion tool
	}

	/**
	 * Converts values in centimeters to meters
	 * @param inCentimeters - the value in centimeters
	 * @return - the value in meters
	 */
	public static double convertCentimetersToMeters(double inCentimeters) {
		return inCentimeters/100.0;
	}

	/**
	 * Converts the heading to a BSM heading.
	 * @param headingClockwiseFromNorth The angle of the car in degrees clockwise from North.
	 * @return The angle of the car in 0.0125 degrees clockwise from North.
	 */
	public static int convertDegreesToBSMHeading(double headingClockwiseFromNorth) {
		return (int) ((headingClockwiseFromNorth % 360.0) / 0.0125);
	}

	/**
	 * Converts values in feet to centimeters
	 * @param inFeet - the value in feet
	 * @return - the value in centimeters
	 */
	public static double convertFeetToCentimeters(double inFeet) {
		return inFeet * 30.48; // Value according to Google's length conversion tool
	}

	/**
	 * Converts values in ft/sec to cm/sec
	 * @param inFeetPerSec - the value in ft/sec
	 * @return - the value in cm/sec
	 */
	public static double convertFeetPerSecondToCentimetersPerSecond(double inFeetPerSec) {
		return convertFeetToCentimeters(inFeetPerSec);
	}

	/**
	 * Converts values in feet to meters
	 * @param inFeet - the value in feet
	 * @return - the value in meters
	 */
	public static double convertFeetToMeters(double inFeet) {
		return inFeet * 0.3048; // Value according to Google's length conversion tool
	}

	/**
	 * Converts values in meters to centimeters
	 * @param inMeters - the value in meters
	 * @return - the value in centimeters
	 */
	public static double convertMetersToCentimeters(double inMeters) {
		return inMeters * 100.0;
	}

	/**
	 * Converts values in meters to feet
	 * @param inMeters - the value in meters
	 * @return - the value in feet
	 */
	public static double convertMetersToFeet(double inMeters) {
		return inMeters * 3.28084; // Value according to Google's length conversion tool
	}

	/**
	 * Converts values in mph to mps.
	 * @param inMPH The value in miles per hour
	 * @return The value in meters per second
	 */
	public static double convertMilesPerHourToMetersPerSecond(double inMPH) {
		return inMPH * 0.44704; // Value according to Google's length conversion tool
	}
	
	/**
	 * Converts values in mps to mph.
	 * @param inMPS The value in meters per second
	 * @return The value in miles per hour
	 */
	public static double convertMetersPerSecondToMilesPerHour(double inMPS) {
		return inMPS * 2.23694; // Value according to Google's length conversion tool
	}

	/**
	 * Converts 1/10th microdegrees into latitude or longitude for use by geolocational services
	 * @param oneTenthMicroDegrees - the value in 1/10th microdegrees
	 * @return - the value in normal degrees
	 */
	public static double convertToDegrees(int oneTenthMicroDegrees) {
		return oneTenthMicroDegrees/10000000.0; // 1/10 microdegrees is 1.0 * 10^-7 degrees
	}

	/**
	 * Converts a latitude or longitude to 1/10th microdegrees for usage in the BSM and Map Data messages
	 * @param angle - the latitude or longitude (in degrees we want to convert)
	 * @return - the 1/10th microdegrees of the angle
	 */
	public static int convertToOneTenthMicrodegree(double angle) {
		return (int)(angle*10000000.0); // 1/10 microdegrees is 1.0 * 10^-7 degrees
	}
	
	/**
	 * Converts a value in seconds to a value in one-tenth-seconds.
	 * @param seconds The value in seconds.
	 * @return The value in one-tenth-seconds.
	 */
	public static double convertSecondsToOneTenthSeconds(double seconds) {
		return seconds * 10.0;
	}
	
	/**
	 * Converts a value in one-tenth-seconds to a value in seconds.
	 * @param oneTenthSeconds The value in one-tenth-seconds.
	 * @return The value in seconds.
	 */
	public static double convertOneTenthSecondsToSeconds(double oneTenthSeconds) {
		return oneTenthSeconds / 10.0;
	}
	
	/**
	 * Converts a value in milliseconds to a value in seconds
	 * @param inMilliseconds The value in milliseconds
	 * @return The value in seconds
	 */
	public static double convertMillisecondsToSeconds(double inMilliseconds) {
		return inMilliseconds / 1000.0;
	}
	
	/**
	 * Converts a value in seconds to a value in milliseconds
	 * @param inSeconds The value in seconds
	 * @return The value in milliseconds
	 */
	public static double convertSecondsToMilliseconds(double inSeconds) {
		return inSeconds * 1000.0;
	}
}
